package com.es.web.product.controller;

import java.util.Map;

import org.apache.commons.beanutils.BeanMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.es.common.cache.GlobalCacheKey;
import com.es.common.utils.StringUtils;
import com.es.search.param.ProductSearchParam;
import com.es.web.product.view.SearchData;

@RestController
@RequestMapping("/product")
public class ProductController {
	/**
	 * 用来绑定单个请求数据，既可以是url中的参数，也可以是表单提交的参数和上传的文件
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/search")
	@ResponseBody
	public ProductSearchParam search(
			@RequestParam(value="ids", required=false) String ids,
			@RequestParam(value="keyword",required=false) String keyword, 
			@RequestParam(value="storeid",required=false) String storeid,
			@RequestParam(value="priceRange",required=false) String priceRange, 
			@RequestParam(value="page",required=false) String page, 
			@RequestParam(value="pageSize",required=false) String pageSize,
			@RequestParam(value="columnid",required=false) String columnid, 
			@RequestParam(value="order",required=false) String order){
		ProductSearchParam param = new ProductSearchParam();
		param.setKeyword(keyword);
		param.setOrderBy(order);
		int per_page = StringUtils.toInt(page,1);
		int size = StringUtils.toInt(pageSize, 10);
		int start = (per_page-1)*size;
		int limit = size;
		param.setStart(start);
		param.setLimit(limit);
		BeanMap beanMap = new BeanMap(param);
		return param;
	}

	
	/**
	 * 接收json数据
	 * @param data
	 * @return
	 */
	@RequestMapping("/search2")
	@ResponseBody
	public ProductSearchParam search2(@RequestBody SearchData data){
		
		ProductSearchParam param = new ProductSearchParam();
		param.setKeyword(data.getKeyword());
		param.setOrderBy(data.getOrder());
		int start = (data.getPage()-1)*data.getPageSize();
		int limit = data.getPageSize();
		param.setStart(start);
		param.setLimit(limit);
		return param;
	}
	
	/**
	 * 将URL中的占位符参数绑定到控制器处理方法的入参
	 * @param mid
	 * @param id
	 * @return
	 */
	@RequestMapping("/details/{mid}/{id}")
	@ResponseBody
	public String details(@PathVariable String mid ,@PathVariable String id){
		System.out.println(mid+"*********"+id);
		String key = String.format(GlobalCacheKey.product, id, mid);
		//Product product = RedisTool.getInstance().get(SpringUtils.getCacheFactory().get(GlobalCacheName.PRODUCT_SHARD.getName()), key, Product.class);
		//obj2map(product);
		/*Product product = new Product();
		Map<String, String> hash = new HashMap<String, String>();
		hash.put(mid, null);
		RedisTool.getInstance().hmset(SpringUtils.getCacheFactory().get(GlobalCacheName.PRODUCT_SHARD.getName()), key, hash);
*/		return "ok";
	}
	
	public Map<Object, Object> obj2map(Object obj){
		BeanMap map = new BeanMap(obj);
		return map;
	}

}
